﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.IsolatedStorage;

namespace Handsfree
{
    public partial class Preferences : Form
    {
        const string ISOLATED_FILE_NAME = "settings.config";
        IsolatedStorageFile isoStore;
        public Preferences()
        {
            InitializeComponent();
            isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
            checkFileExists();
            loadDefaults();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Hide();
            //Reset Values to Previously Saved Values
            loadDefaults();
        }

        private void Save_Click(object sender, EventArgs e)
        {
            saveValues();
        }

        private void loadDefaults()
        {
            IsolatedStorageFileStream iStream = new IsolatedStorageFileStream(ISOLATED_FILE_NAME, FileMode.Open, isoStore);
            StreamReader reader = new StreamReader(iStream);
            String line;
            int count = 0;
            while ((line = reader.ReadLine()) != null)
            {
                count++;
                switch (count)
                {
                    case 1:
                        robustnessTrackbar.Value = Convert.ToInt32(line);
                        break;
                    case 2:
                        speedTrackbar.Value = Convert.ToInt32(line);
                        break;
                    case 3:
                        onloadCheckBox.Checked = Convert.ToBoolean(line);
                        break;
                    case 4:
                        textBox1.Text = line;
                        break;
                    default:
                        //MessageBox.Show("Too many Data Points"+count.ToString()+" "+line);
                        break;
                }
            }

            reader.Close();
        }

        private void saveValues()
        {
            /*
             * Saves the values of various settings
             * For more information see documentation at checkFileExists()
             */
            try
            {
                IsolatedStorageFileStream oStream = new IsolatedStorageFileStream(ISOLATED_FILE_NAME, FileMode.Create, isoStore);
                StreamWriter writer = new StreamWriter(oStream);
                writer.WriteLine(robustnessTrackbar.Value.ToString());
                writer.WriteLine(speedTrackbar.Value.ToString());
                writer.WriteLine(onloadCheckBox.Checked.ToString());
                writer.WriteLine(textBox1.Text);
                writer.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            this.Hide();
        }

        private void checkFileExists()
        {
            /*
             * Checks whether the settings.config file exists in storage or not
             * If not, then creates the file with the default values as specified below (in the order specifiied below)
             * Also, the file only contains the values, not the keys
             * Robustness: 70
             * Speed: 1
             * OnStart: true
             * Default Path:(blank)
             */

            // Check if the file already exists in isolated storage.
            string[] fileNames = isoStore.GetFileNames(ISOLATED_FILE_NAME);
            bool exists = false;
            foreach (string file in fileNames)
            {
                if (file == ISOLATED_FILE_NAME)
                {
                    exists = true;
                }
            }
            if (!exists)
            {
                IsolatedStorageFileStream oStream = new IsolatedStorageFileStream(ISOLATED_FILE_NAME, FileMode.Create, isoStore);
                StreamWriter writer = new StreamWriter(oStream);
                writer.WriteLine("70");//Robustness
                writer.WriteLine("1");//Speed
                writer.WriteLine("true");//Indicates Start Engines on Load onLoad
                writer.WriteLine("");//Default Path
                writer.Close();
            }
        }
        public float getRobustnessValue()
        {
            return robustnessTrackbar.Value/100;
        }
        public int getSpeedValue()
        {
            return speedTrackbar.Value;
        }
        public bool onStartup()
        {
            return onloadCheckBox.Checked;
        }
        public string getPath()
        {
            if(textBox1.Text!="")
             return textBox1.Text;
            return "Commands.sf";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            textBox1.Text = openFileDialog1.FileName;
        }

    }
}
